package com.amado.course42Nio.demo1;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @author wangyulong
 * @version 1.5.0
 * @date 2020/3/6 22:27
 */
public class ChannelOne {
    public static void main(String[] args) throws IOException {
        FileInputStream fileInputStream = new FileInputStream("C:\\temp\\temp.txt");
        FileOutputStream fileOutputStream = new FileOutputStream("C:\\temp\\temp_cp.txt");
        ByteBuffer byteBuffer = ByteBuffer.allocate(200);
        FileChannel inputChannel = fileInputStream.getChannel();
        FileChannel outputChannel = fileOutputStream.getChannel();
        while(inputChannel.read(byteBuffer)!=-1){
            //反转
            System.out.println(byteBuffer+"--反转前");
            byteBuffer.flip();
            System.out.println(byteBuffer+"--反转后");
            //开始写
            outputChannel.write(byteBuffer);
            System.out.println(byteBuffer+"--写后");
            //反转
            byteBuffer.flip();
            System.out.println(byteBuffer+"--写后反转");
        }
        inputChannel.close();
        outputChannel.close();
    }
}
